package com.spudtech.graphics
{
    
    import flash.geom.*;
    import flash.display.*;
    
    import com.spudtech.*;
    import com.spudtech.tween.*;
    
    
    public class SpudAnimBone extends SpudGroup implements ISpudDrawable
    {
        
        protected var _graphic : SpudGraphic;
        
        
        public function SpudAnimBone(bone_name : String,
                                     position : Point,
                                     bone_graphic : SpudGraphic = null)
        {
            super();
            
            this.name = bone_name;
            
            this.x = position.x;
            this.y = position.y;
            
            // So that the setter doesn't freak out
            this._graphic = null;
            
            // Actually set the graphic
            this.graphic = bone_graphic;
            
            var because : SpudNumberTween;
            var yeah : SpudPositionTween;
            //var what_now : SpudAngleTween;
            
        }
        
        public function get graphic() : SpudGraphic
        {
            return _graphic;
        }
        public function set graphic(new_graphic : SpudGraphic) : void
        {
            if (new_graphic == _graphic)
            {
                // That is already our graphic!
                return;
            }
            
            if (_graphic != null)
            {
                // We have a graphic right now
                // Remove it
                
                _graphic.destroy();
                
                removeChild(_graphic);
                
            }
            
            _graphic = new_graphic;
            
            if (_graphic != null)
            {
                // We have a new graphic
                // Attach it
                
                addChild(_graphic);
                
                _graphic.begin();
                
            }
            
        }
        
        
        public function get membersBelow() : Array
        {
            // members with a positive layer
            // value are below the caller
            
            var is_below : Function = function(item : SpudObject, index : int, array : Array) : Boolean
            {
                if (item.layer > 0)
                {
                    // It is below
                    return true;
                }
                else
                {
                    // It is above
                    return false;
                }
            }
            
            return members.filter(is_below);
            
        }
        
        public function get membersAbove() : Array
        {
            // Members with negative of zero
            // layer values are above the
            // caller
            
            var is_above : Function = function(item : SpudObject, index : int, array : Array) : Boolean
            {
                if (item.layer <= 0)
                {
                    // It is above
                    return true;
                }
                else
                {
                    // It is below
                    return false;
                }
                
            }
            
            return members.filter(is_above);
            
        }
        
        
        override public function render(offset : Matrix,
                                        clip_rect : Rectangle,
                                        draw_graphics : Graphics) : void
        {
            var child_offset : Matrix = offset.clone();
            
            
            var below : Array = membersBelow.sort("layer",[Array.DESCENDING,Array.RETURNINDEXEDARRAY]);
            var above : Array = membersAbove.sort("layer",[Array.DESCENDING,Array.RETURNINDEXEDARRAY]);
            
            
            // Render children below us
            
            for each (var bone : SpudObject in below)
            {
                bone.render(child_offset,clip_rect,draw_graphics);
            }
            
            // Here, we render out graphic
            // If you REALLY want to know
            // how this works, look at
            // SpudEntity.render() because
            // this is basically the same
            // thing
            
            var graphic_offset : Matrix = offset.clone();
            
            var graphic_pos : Point = new Point(0,0);
            
            graphic_pos = graphic.localToGlobal(graphic_pos);
            
            graphic_offset.translate(
                    graphic_pos.x,
                    graphic_pos.y
            );
            
            
            graphic.render(
                graphic_offset,
                clip_rect,
                draw_graphics
            )
            
            
            // Render child above us
            
            for each (bone in above)
            {
                bone.render(child_offset,clip_rect,draw_graphics);
            }
            
            
            
        }
        
    }
    
}
